/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package backend;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

@ManagedBean(name="add_event")
@RequestScoped
public class add_employee {
    private String starttime;
    private String endtime;
    private int duration;
    private String supervisor;
    private String eventname;
    private String assignees;
    public ArrayList userList;
    public Connection con;
    public Statement statement;
    public int updateQuery = 0;
    public String SQL_Str;
    public ResultSet rs;
    public String s;
    public int sa;
    public String create(String Starttime,String Endtime,int Duration, String Supervisor,String Eventname, String Assignees)
    {
        userList = getUserList();
        boolean vaildUser = false;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_jsf","root","");
            statement = con.createStatement();
            for(int i =0;i<Assignees.split(",").length;i++ ){
                s = Assignees.split(",")[i].replace(" ", "");
                if (userList.contains(s)){
                        vaildUser = true;
                }
                else{
                    vaildUser = false;
                }
                
            }
            if(vaildUser){
                SQL_Str="INSERT INTO db_jsf.event (StartTime,EndTime,Duration,Supervisor,EventName,Assignees) VALUES ('" + Starttime +"','" + Endtime +"','" + Duration +"','" + Supervisor +"','" + Eventname +"','" + Assignees +"')";
                updateQuery = statement.executeUpdate(SQL_Str);
                System.out.println("added");
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
        }
        if(updateQuery!=0){
            return "inserted";
        }
        else{
            return "error";
        }
    }
    public ArrayList getUserList(){
        userList = new ArrayList();
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_jsf","root","");
            statement = con.createStatement();
            SQL_Str="SELECT * FROM db_jsf.tbl_user";
            rs=statement.executeQuery(SQL_Str);
            while(rs.next()){
                s = rs.getString(4).toString();
                if(s.equals("User")){
                    userList.add(rs.getString(2).toString());
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
        }
        return userList;
    }
    public String getStarttime() {
        return starttime;
    }

    public void setStarttime(String starttime) {
        this.starttime = starttime;
    }

    public String getEndtime() {
        return endtime;
    }

    public void setEndtime(String endtime) {
        this.endtime = endtime;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getSupervisor() {
        return supervisor;
    }

    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }

    public String getEventname() {
        return eventname;
    }

    public void setEventname(String eventname) {
        this.eventname = eventname;
    }

    public String getAssignees() {
        return assignees;
    }

    public void setAssignees(String assignees) {
        this.assignees = assignees;
    }

    public Statement getStatement() {
        return statement;
    }

    public void setStatement(Statement statement) {
        this.statement = statement;
    }
    
    public String submitEvent()
    {
        return create(starttime,endtime,duration, supervisor,eventname, assignees);
    }
}
